﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BillReference.aspx.cs" Inherits="Main_Accounts_VoucherEntry_BillReference" Culture="en-GB" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxControl" %>
<%@ Register Assembly="CalendarExtenderPlus" Namespace="AjaxControlToolkitPlus" TagPrefix="advancedCalender" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Bill Reference</title>
       <link href="../../../Styles/accounts.css" rel="stylesheet" type="text/css" />
     <script type="text/javascript">

         function isNumberKey(control_id, evt) {
             var charCode = (evt.which) ? evt.which : event.keyCode;
             if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 45 || charCode > 47))
                 return false;

             if (charCode == 46) {
                 var dotcount = document.getElementById(control_id).value.split('.').length - 1;
                 if (dotcount >= 1) {
                     return false;
                 }
             }
             return true;
         }
     
         function SetZeroOnBlurDebit() {
             var totalDebitAmt = 0;
             var Count = document.getElementById("hidControl").value;
             for (var i = 1; i <= Count; i++) {
                 var zero = 0;
                 var txtDebit = document.getElementById("txtDebit" + i);
                 if (txtDebit.value == '') {
                     if (txtDebit.disabled == false) {
                         txtDebit.value = zero.toFixed(2);
                         totalDebitAmt += zero;
                     }
                     else {
                         totalDebitAmt += zero;
                     }
                 }
                 else {
                     var debitAmt = parseFloat(txtDebit.value);
                     txtDebit.value = debitAmt.toFixed(2);
                     totalDebitAmt += debitAmt;
                 }
             }
             document.getElementById("txtTotalDebit").value = totalDebitAmt.toFixed(2);

         }

         function SetZeroOnBlurCredit() {
             var totalCreditAmt = 0;
             var Count = document.getElementById("hidControl").value;
             for (var i = 1; i <= Count; i++) {

                 var zero = 0;
                 var txtCredit = document.getElementById("txtCredit" + i);
                 if (txtCredit.value == '') {
                     if (txtCredit.disabled == false) {
                         txtCredit.value = zero.toFixed(2);
                         totalCreditAmt += zero;
                     }
                     else {
                         totalCreditAmt += zero;
                     }
                 }
                 else {
                     var creditAmt = parseFloat(txtCredit.value);
                     txtCredit.value = creditAmt.toFixed(2);
                     totalCreditAmt += creditAmt;
                 }
             }
             document.getElementById("txtTotalCredit").value = totalCreditAmt.toFixed(2);

         }

         function RemoveZeroOnFocusCredit(ctl_id) {
             var totalCreditAmt = document.getElementById("txtTotalCredit").value;
             var totalDebitAmt = document.getElementById("txtTotalDebit").value;
             var txtCredit = document.getElementById(ctl_id);
             if (txtCredit.value == '0.00' && txtCredit.disabled != true) {
                 if (parseFloat(totalDebitAmt) > parseFloat(totalCreditAmt)) {
                     txtCredit.value = parseFloat(parseFloat(totalDebitAmt) - parseFloat(totalCreditAmt)).toFixed(2);
                 }
                 else if (parseFloat(totalDebitAmt) == parseFloat(totalCreditAmt)) {
                     txtCredit.value = '';
                 }
                 else {
                     txtCredit.value = '';
                 }
             }
         }
         function RemoveZeroOnFocusDebit(ctl_id) {
             var totalCreditAmt = document.getElementById("txtTotalCredit").value;
             var totalDebitAmt = document.getElementById("txtTotalDebit").value;
             var txtDebit = document.getElementById(ctl_id);
             if (txtDebit.value == '0.00' && txtDebit.disabled != true) {
                 if (parseFloat(totalCreditAmt) > parseFloat(totalDebitAmt)) {
                     txtDebit.value = parseFloat(parseFloat(totalCreditAmt) - parseFloat(totalDebitAmt)).toFixed(2);
                 }
                 else if (parseFloat(totalCreditAmt) == parseFloat(totalDebitAmt)) {
                     txtDebit.value = '';
                 }
                 else {
                     txtDebit.value = '';
                 }
             }
         }
       
        function ValidateRepeatLedger(id) {           
            var flag = true;
            var count = document.getElementById("hidControl").value;
            var currentLedger = document.getElementById(id);
            var lastNo = id.toString().substring(13);
            var currentValue = currentLedger.options[currentLedger.selectedIndex].value;
            for (var i = 1; i <= count; i++) {
                var previousLedger = document.getElementById("Ddlparticular" + i);
                if (previousLedger != null) {
                    var previousValue = previousLedger.options[previousLedger.selectedIndex].value;
                    if (currentLedger != previousLedger) {
                        if (currentValue == previousValue && previousValue != '-1' && currentValue != '-1') {
                            alert('Specifed particular can not be same');
                            currentLedger.selectedIndex = '0';
                            flag = false;
                            break;
                        }
                    }
                }
            }

            if (flag == true) {
                setTimeout('__doPostBack(\'' + id + '\',\'\')', 0);
            }
            return flag;
        }

        function sumDr() {
            var totalDebitAmt = 0;
            var Count = document.getElementById("hidControl").value;
            
            for (var i = 1; i <= Count; i++) {
                var txtDebit = document.getElementById("TxtDebit" + i);
                if (txtDebit.value != '') {
                    if (txtDebit.disabled != true) {
                        var debitAmt = parseFloat(txtDebit.value);
                        txtDebit.value = debitAmt.toFixed(2);
                        totalDebitAmt += debitAmt;
                    }
                }
            }
            document.getElementById("txtTotalDebit").value = totalDebitAmt.toFixed(2);
        }

        function sumCr() {
            var txtTotalCreditAmt = 0;
            var Count = document.getElementById("hidControl").value;

            for (var i = 1; i <= Count; i++) {
                var txtCredit = document.getElementById("TxtCredit" + i);
                if (txtCredit.value != '') {
                    if (txtCredit.disabled != true) {
                        var CreditAmt = parseFloat(txtCredit.value);
                        txtCredit.value = CreditAmt.toFixed(2);
                        txtTotalCreditAmt += CreditAmt;
                    }
                }
            }
            document.getElementById("txtTotalCredit").value = txtTotalCreditAmt.toFixed(2);
        }

        function ValidateNewRow() {
            var flag = true;
            var rowCount = document.getElementById("hidControl").value;
            var ddlDrCr = document.getElementById("ddlDrCr" + rowCount);
            var Txtparticular = document.getElementById("Txtparticular" + rowCount);
            var txtDebit = document.getElementById("TxtDebit" + rowCount);
            var txtCredit = document.getElementById("TxtCredit" + rowCount);
            if (Txtparticular.value == '') {
                alert('Specify Particular First');
                flag = false;
                document.getElementById("Txtparticular" + rowCount).focus();
                
            }
            else if (ddlDrCr.value == '1' && (txtDebit.value == '0.00' || txtDebit.value == '')) {
                alert('Specify Debit Amount');
                flag = false;
                document.getElementById("TxtDebit" + rowCount).focus();
            }
            else if (ddlDrCr.value == '2' && (txtCredit.value == '0.00' || txtCredit.value == '')) {
                alert('Specify Credit First');
                flag = false;
                document.getElementById("TxtCredit" + rowCount).focus();
            }
            return flag;
        }

        function retransfervalue() {
            var Count = document.getElementById("hidControl").value;
            var sumdrAmt = 0;
            var SumCrAmt = 0;
            var Selectedtype = "";
            var Selecteddebit = "";
            var Selectedcredit = "";
            var Txttype = "";
            var Selecteddrcr = "";
            var Selectparticular = "";
            var SelectTxtParticular = "";
            var SelectedDate = "";
            var RefText = "";
            var dramt = "";
          
             for (var i = 1; i <= Count; i++) {
                 var txtDebit = 0;
                 if (document.getElementById("TxtDebit" + i).value != "") {
                     txtDebit = parseFloat(document.getElementById("TxtDebit" + i).value);
                 }
                 sumdrAmt += txtDebit;
                 
                 var txtCredit = 0;
                 if (document.getElementById("TxtCredit" + i).value != "") {
                    txtCredit= parseFloat(document.getElementById("TxtCredit" + i).value);
                 }
                 SumCrAmt += txtCredit;
                 
                 var currentLedger = document.getElementById("ddltype" + i); 
                 var currentValue = currentLedger.options[currentLedger.selectedIndex].value;
                 Selectedtype += currentValue + "|";

                 var drCr = document.getElementById("ddlDrCr" + i);
                 var drCrValue = drCr.options[drCr.selectedIndex].value;
                 Selecteddrcr += drCrValue + "|";

                 var particular = document.getElementById("Ddlparticular" + i);
                 if (particular != null) {
                     var particularValue = particular.options[particular.selectedIndex].value;
                     RefText += particular.options[particular.selectedIndex].text + ",";
                     Selectparticular += particularValue + "|";
                 }
                 else {
                     Selectparticular += " |";
                     RefText += " ,";
                }
                var Txtparticular = document.getElementById("Txtparticular" + i);
                if (Txtparticular != null) {
                    SelectTxtParticular += Txtparticular.value + "-";
                }
                else {
                    SelectTxtParticular += " -";
                }
                //alert(Selectparticular);
                SelectedDate += document.getElementById("TxtDate" + i).value + "|";

                Selectedcredit += document.getElementById("TxtCredit" + i).value + "|";
                
                 Selecteddebit += document.getElementById("TxtDebit" + i).value+"|";
                 
             }
             dramt = Selecteddebit + ":" + Selectedtype + ":" + Selecteddrcr + ":" + Selectparticular + ":" + SelectTxtParticular + ":" + SelectedDate + ":" + RefText + ":" + Selectedcredit;
             
             var hidamt = document.getElementById("Hidbillamt").value;
             var drctrlid = document.getElementById("HidSum").value;
             var DrAmt = document.getElementById("HidDrAmt").value;
             var FinalValue = parseFloat(DrAmt) + parseFloat(SumCrAmt);

             if (DrAmt == sumdrAmt || sumdrAmt == FinalValue) {
               // window.opener.document.getElementById(drctrlid).value = sumdr;
             window.opener.document.getElementById(hidamt).value =dramt ;
              alert(dramt);
           self.close();
             }
             else {
                 alert("Amount not matching");
             }
        }
        function validation_valuetransfer() {
            if (ValidateNewRow() == true){
                if (retransfervalue() == true) {
                    return true;
                }
                else {
                    return false;
                }
            }
            else {
                return false;
            }
            
        }
      
      </script>
      
</head>
<body>
    <form id="form1" runat="server">
         <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
    <div align="center">
           <asp:label ID="lblmsg" runat="server" EnableViewState="false"></asp:label>
    </div>
    <asp:HiddenField ID="hidControl" runat="server" Value="2" />
    <asp:HiddenField ID="HidSum" runat="server" />
    <asp:HiddenField ID="HidDrAmt" runat="server" />
    <asp:HiddenField ID="Hidbillamt" runat="server" />
    <asp:HiddenField ID="HidDate" runat="server" />
    <asp:HiddenField ID="HidLid" runat="server" />
    <asp:HiddenField ID="HidLname" runat="server" />
    
    <table width="100%"  cellpadding="2" cellspacing="0" style="border:solid 1px #818181;margin-bottom:15px">
        <tr>
            <td colspan="6" class="headtd"> Bill Details            
            </td>
        </tr>
        <tr>
            <td colspan="4">
             
            </td>  
            <td colspan="2">
                &nbsp;
            </td>
        </tr>
        <tr>
            <td colspan="4" style="width:78%;">
                <asp:TextBox ID="Txtledger" Width="100%" runat="server"></asp:TextBox>
            </td>
            <td colspan="2" style="width:20%; margin-left:10px;">
              <asp:TextBox ID="Txtamt" Width="100%" runat="server"></asp:TextBox>
                <asp:HiddenField ID="Hidamt" runat="server" />
            </td>
        </tr>
        <tr>
            <td colspan="6" > 
        <table id="tblVouEntry" runat="server" width="100%"  cellpadding="2" cellspacing="0" style="border:solid 1px #818181;margin-bottom:15px">
             <tr>
            <td style="text-align:center; width:10%; border-bottom:solid 1px #818181; border-right:solid 1px #818181;" class="headtd">
                 Type  
            </td>
            <td style="text-align:center;width:10%; border-bottom:solid 1px #818181; border-right:solid 1px #818181;" class="headtd">
                 Dr/Cr
            </td>
            <td style="text-align:center; width:35%; border-bottom:solid 1px #818181; border-right:solid 1px #818181;" class="headtd">
                Particular   
            </td>
            <td style="text-align:center; width:15%; border-bottom:solid 1px #818181; border-right:solid 1px #818181;" class="headtd">
                Date
            </td>
            <td style="text-align:center; width:15%; border-bottom:solid 1px #818181; border-right:solid 1px #818181;" class="headtd">
                Debit 
            </td>
            <td style="text-align:center; width:15%; border-bottom:solid 1px #818181; border-right:solid 1px #818181;" class="headtd">
                Credit
            </td>
        </tr>
           <tr>
                <td>
                    <asp:DropDownList ID="ddltype1" runat="server" CssClass="dropdown" Width="98%" 
                        AutoPostBack="true" ToolTip="Type" 
                        onselectedindexchanged="ddltype_SelectedIndexChanged">
                        <asp:ListItem Text="New Bill" Selected="True" Value="1"></asp:ListItem>
                        <asp:ListItem Text="Towards Bill" Value="2"></asp:ListItem>
                        <asp:ListItem Text="On A/c" Value="3"></asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td>
                   <asp:DropDownList ID="ddlDrCr1" runat="server" CssClass="dropdown" Width="98%" 
                        AutoPostBack="true" ToolTip="Debit/Credit" 
                        onselectedindexchanged="ddlDrCr_SelectedIndexChanged">
                        <asp:ListItem Text="Dr"  Value="1"></asp:ListItem>
                        <asp:ListItem Text="Cr"  Value="2"></asp:ListItem>
                   </asp:DropDownList>
                </td>
                <td>
                    <asp:TextBox ID="Txtparticular1" runat="server" CssClass="inputnumber" Width="98%" Visible="true" Enabled="true" ToolTip="Particular" ></asp:TextBox>
                    <asp:DropDownList ID="Ddlparticular1" runat="server" Visible="false" CssClass="dropdown" Width="98%" onchange="return ValidateRepeatLedger(this.id);"  ToolTip="Particular" ></asp:DropDownList>
                </td>
                <td>
                    <asp:TextBox ID="TxtDate1" runat="server" CssClass="inputnumber" Width="98%" ToolTip="Date" Enabled="true" ></asp:TextBox>
                    <advancedCalender:CalendarExtenderPlus ID="CalendarExtenderPlus2" runat="server"  PopupButtonID="imgDate"  Format="dd/MM/yyyy" 
                    TargetControlID="TxtDate1">
                    </advancedCalender:CalendarExtenderPlus>
       
                </td>
                <td>
                    <asp:TextBox ID="TxtDebit1" runat="server" onfocus="return RemoveZeroOnFocusDebit(this.id);" onblur="return SetZeroOnBlurDebit(), sumDr();" Text="0.00" CssClass="inputnumber" Width="98%" ToolTip="DebitAmount" ></asp:TextBox>
                    <asp:HiddenField ID="Hiddebit1" runat="server" />
                </td>
               <td>
                    <asp:TextBox ID="TxtCredit1" runat="server" CssClass="inputnumber" Width="98%" onfocus="return RemoveZeroOnFocusCredit(this.id);" onblur="return SetZeroOnBlurCredit(), sumCr();" ToolTip="Credit Amount" Enabled="false" ></asp:TextBox>
                    <asp:HiddenField ID="Hidcredit1" runat="server" />
                </td>
            </tr>
             <tr>
                <td>
                    <asp:DropDownList ID="ddltype2" runat="server" CssClass="dropdown" Width="98%" AutoPostBack="true" ToolTip="Type"
                     onselectedindexchanged="ddltype_SelectedIndexChanged">
                         <asp:ListItem Text="New Bill" Selected="True" Value="1"></asp:ListItem>
                        <asp:ListItem Text="Towards Bill" Value="2"></asp:ListItem>
                        <asp:ListItem Text="On A/c" Value="3"></asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td>
                   <asp:DropDownList ID="ddlDrCr2" runat="server" CssClass="dropdown" Width="98%" AutoPostBack="true" ToolTip="Debit/Credit"
                   onselectedindexchanged="ddlDrCr_SelectedIndexChanged" >
                        <asp:ListItem Text="Dr"  Value="1"></asp:ListItem>
                        <asp:ListItem Text="Cr" Selected="True" Value="2"></asp:ListItem>
                   </asp:DropDownList>
                </td>
                <td>
                    <asp:TextBox ID="Txtparticular2" runat="server" CssClass="inputnumber" Width="98%" ToolTip="Particular" ></asp:TextBox>
                    <asp:DropDownList ID="Ddlparticular2" runat="server" Visible="false" onchange="return ValidateRepeatLedger(this.id);"  CssClass="dropdown" Width="98%" ToolTip="Particular" ></asp:DropDownList>
                </td>
                <td>
                    <asp:TextBox ID="TxtDate2" runat="server" CssClass="inputnumber" Width="98%" ToolTip="Date" Enabled="true" ></asp:TextBox>
                    <advancedCalender:CalendarExtenderPlus ID="CalendarExtenderPlus1" runat="server"  PopupButtonID="imgDate"  Format="dd/MM/yyyy" 
                    TargetControlID="TxtDate2">
                    </advancedCalender:CalendarExtenderPlus>
                </td>
                <td>
                    <asp:TextBox ID="TxtDebit2" runat="server" CssClass="inputnumber" Width="98%" onblur="sumDr();" ToolTip="DebitAmount" Enabled="false" ></asp:TextBox>
                    <asp:HiddenField ID="Hiddebit2" runat="server" />
                </td>
               <td>
                    <asp:TextBox ID="TxtCredit2" runat="server" CssClass="inputnumber" Width="98%" onblur="sumCr();" ToolTip="Credit Amount" Enabled="false" ></asp:TextBox>
                    <asp:HiddenField ID="Hidcredit2" runat="server" />
                </td>
            </tr>
        </table> 
            </td>
        </tr>  
        
         <tr>
            <td colspan="6" style="border:solid 1px #818181; background-color:#f2f2f2">   
                <table width="100%" cellpadding="2" cellspacing="0">
                 <tr>
                        <td style="text-align:right;width:70%;">
                          &nbsp;
                        </td>
                        <td style="text-align:center;width:15%;">
                            <asp:TextBox ID="txtTotalDebit" runat="server" CssClass="inputnumber" Width="98%" ToolTip="Total Debit Amount" Text="0.00" onkeypress="return false" onkeydown="return false"></asp:TextBox>
                        </td>
                        <td style="text-align:center; width:15%;">
                            <asp:TextBox ID="txtTotalCredit" runat="server" CssClass="inputnumber" Width="98%" ToolTip="Total Credit Amount" Text="0.00" onkeypress="return false" onkeydown="return false"></asp:TextBox>
                        </td>
                       
                    </tr> 
                    <tr>
                        <td colspan="3" style="text-align:center;width:100%;">
                              <asp:Button ID="btnAddRow" runat="server"  ToolTip="Add New Row!"  
                                Text="Add Row" CssClass="button" onclick="btnAddRow_Click" />&nbsp;
                            <asp:Button ID="btnDeleteRow" runat="server" ToolTip="Delete Last Row!" 
                                  Text="Delete Row" CssClass="button1" onclick="btnDeleteRow_Click"  />&nbsp;
                            <asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="return validation_valuetransfer();"
                                  ToolTip="Save Contra Transaction!" CssClass="button" onclick="btnSave_Click" />&nbsp;
                            <asp:Button ID="BtnClose" runat="server" Text="Close" ToolTip="Close Current Page!"  CssClass="button"  />&nbsp;
                        </td>
                    </tr>                                           
                </table>  
             </td>
         </tr>
    </table>
    </div>
    </form>
</body>
</html>
